package com.wu.foodsafetyplayer.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.bean.MonitorVideoBean;
import com.wu.foodsafetyplayer.app.bean.UserEntity;
import com.wu.foodsafetyplayer.app.dialog.LoadingUtil;
import com.wu.foodsafetyplayer.http.CommonOkHttpClient;
import com.wu.foodsafetyplayer.utils.ACache;
import com.wu.foodsafetyplayer.utils.SpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {
    EditText editName;
    EditText editPass;
    TextView btn_login, btn_register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        editName = this.findViewById(R.id.edit_phonesss);
        editPass = this.findViewById(R.id.edit_pwdssss);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editName.getText().toString().trim().isEmpty() || editPass.getText().toString().trim().isEmpty()) {
                    Toast.makeText(LoginActivity.this, "登录信息不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    String name = editName.getText().toString().trim();
                    String pass = editName.getText().toString().trim();
                    toLogin(name, pass);
                }
            }
        });

        btn_register = findViewById(R.id.btn_register);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(LoginActivity.this, "当前吴权限新建用户，请联系管理员获取帐号密码", Toast.LENGTH_SHORT).show();
            }
        });
    }

    UserEntity userEntity;

    private void toLogin(String name, String pass) {
        LoadingUtil.INSTANCE.showLoading(LoginActivity.this, "加载中");

        CommonOkHttpClient.FUNC_TASK.execute(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();

                Request request = new Request.Builder()
                        .url("http://daxun.tpddns.cn:81/LoginToService?username=" + name + "&password=" + pass)
                        .method("GET", null)
                        .addHeader("content-type", "form-data")
                        .addHeader("Cookie", "IDHTTPSESSIONID=r4EN2qalGkkbZUS")
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String res2 = response.body().string();
                    if (!TextUtils.isEmpty(res2)) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                            }
                        });
                        JSONObject jsonObject = new JSONObject(res2);
                        int result = jsonObject.optInt("result");
                        String token = jsonObject.optString("token");
                        userEntity = new UserEntity();
                        userEntity.setResult(result);
                        userEntity.setToken(token);
                        userEntity.setIsnoLogin(false);

                        SpUtil.INSTANCE.saveUserInfo(userEntity);

                        ACache.get(LoginActivity.this).put("name", name);
                        ACache.get(LoginActivity.this).put("pass", pass);

                        startActivity(new Intent(LoginActivity.this, AppMainActivity.class));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassCastException e) {
                    e.printStackTrace();
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                LoadingUtil.INSTANCE.dismissLoading(LoginActivity.this);
            }
        });
    }

}
